package src_6762_6771;

import java.io.*;
import java.util.Scanner;

public class MathExam6762{
	private static int num1,num2;
	static int grade,n;
	private static String str;
	static StringBuffer results = new StringBuffer();

	public static void main(String[] args) throws Exception{
		
		String subjectNum = getParameter();
		String gradeNum = getParameter2();
		judgeNum(subjectNum,gradeNum);
		n = Integer.parseInt(subjectNum);
		grade = Integer.parseInt(gradeNum);
		getTest();	
		writeToFile();

	}
	
	/**
	 * getParameter方法
	 * 获取题数输入
	 * @param 
	 * @return n
	 */
	public static  String getParameter(){
	    System.out.println("请输入题目数量：");
		Scanner sc = new Scanner(System.in);
		String n = sc.nextLine();
	    return n;
	}
	
	/**
	 * getParameter2方法
	 * 获取年级输入
	 * @param 
	 * @return grade
	 */
	public static String getParameter2(){
		System.out.println("小学一或二年级加减乘除计算题");
		Scanner sc = new Scanner(System.in);
		String  grade = sc.nextLine();
		return grade;
	}
	
	/**
	 * getRandom()方法
	 * 随机生成随机数
	 * @param 
	 * @return null
	 */
	public static void getRandom(){
		 num1 = (int)(Math.random()*100+1);
         num2 = (int)(Math.random()*100+1);
	}
	

	/**
	 * getTest()方法
	 * 生成两个随机数的四则运算
	 * @param 
	 * @return null
	 */
	public static void getTest() {
	
			
		for(int i = 1;i<=n;i++){
			getRandom();
			if(grade==1){
				//小学一年级
				if(num1>num2){
					subNum(i);
					
				}else{
					addNum(i);
				}
			}else{
				//小学二年级的
				if(num1>num2){
					divNum(i);
				}else{
					mulNum(i);
				}
			}
			//字符串的拼接
			results.append(str + "\r\n");
		}
		
		
	}
	/**
	 * mulNum()方法
	 * 乘法运算
	 * @param  i
	 * @return null
	 */
	public static String mulNum(int i) {
		
		System.out.println("("+i +") " + num1 +" * "+ num2 + " =");
		str = ("("+ i +") " + num1 +" * "+ num2 + " = " + (mulNumResult()));
		//写入数据
		return str;
	}
	
	public static int mulNumResult(){
		return num1*num2;
	}
	
	/**
	 * divNum方法
	 * 除法运算
	 * @param  i
	 * @return null
	 */
	public static String divNum(int i) {
		System.out.println("("+ i +") " + num1 +" / "+ num2 + " =");
   	 	if(num1%num2==0){
   	 	 str = ("("+ i +") " + num1 +" / "+ num2 + " = " + (subNumResult()));
   	 	//写入数据
        return str;
   	 	}else{
   		str = ("("+ i +") " + num1 +" / "+ num2 + " = " + (subNumResult()) + "..."+ (num1%num2));
   		//写入数据
	     return str;  
   	 }		
	}
	public static int divNumResult(){
		return num1/num2;
	}
	
	/**
	 * addNum方法
	 * 加法运算
	 * @param  i
	 * @return null
	 */
	public static String addNum(int i) {
	
		System.out.println("("+ i +") " + num1 +" + "+ num2 + " =");
		str = ("("+ i +") " + num1 +" + "+ num2 + " = " + (addNumResult()));
		//写入数据
		return str;
		
	}
	public static int addNumResult(){
		return num1+num2;
	}
	
	/**
	 * subNum方法
	 * 减法运算
	 * @param  i
	 * @return null
	 */
	public static  String subNum(int i) {
		
		System.out.println("("+ i +") " + num1 +" - "+ num2 + " =");
        str = ("("+ i +") " + num1 +" - "+ num2 + " = " + (subNumResult()));
        return str;      	
	}
	public static int subNumResult(){
		return num1-num2;
	}
	
	//把结果和题目写入out.txt文件
	public static void writeToFile()throws Exception{
		System.out.println("------------------标准答案------------------");
		//创建文件
		File f = new File("F:\\eclipsedocument\\Lesson06_6762\\out.txt");
    	if(!f.exists()){
    	    File pf = f.getParentFile();
    	    if(pf !=null && !pf.exists())
    	    	pf.mkdirs();
    		f.createNewFile();
        }
		//创建指向文件的数据输出流
	    DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
	    String text = results.toString();
	    out.writeUTF(text);
	    
	    out.close();   	    
	    //创建指向文件的数据输入流
	    DataInputStream in = new DataInputStream(new FileInputStream(f));
	    //读数据
	    System.out.println(in.readUTF());
	    in.close();
	    System.out.println("请在out.txt文件中查看题目");
	}
	
	//报错处理
	public static void judgeNum(String subjectNum,String gradeNum){
    	//题目和年级不能为空
    	if(subjectNum.length() == 0 || gradeNum.length() == 0)
    		throw new IllegalArgumentException("不能输入零个参数");
    	//题目的个数不能大于三位数
    	if(subjectNum.length()>3)
    	     throw new IllegalArgumentException("参数过大");
    	//题目不能为字母
    	for(int i = 0;(subjectNum.charAt(i)<'0' || subjectNum.charAt(i)>'9')&&(i<subjectNum.length());i++)
    		throw new IllegalArgumentException("不要输入非数字");
    	if(gradeNum.charAt(0) <= '0' || gradeNum.charAt(0) >= '3' || gradeNum.length() > 1)
    		throw new  IllegalArgumentException("年级只能是一和二");
}
	/**
	 * 静态获取数
	 * 减法运算
	 * @param  nummber1 nummber2
	 * @return null
	 */
	public static void setNummber(int nummber1,int nummber2){
		num1 = nummber1;
		num2 = nummber2;
	}
}